package object;

/**
 * 面向对象编程(基础部分)练习
 */
public class ObjectExercise02 {

    public static void main(String[] args) {
        /*
            1)编写类A03,实现数组的复制功能copyArr,输入旧数组，返回新数组，元素和旧数组一样
            2)定义一个圆类Circle，属性：半径，提供显示圆周长的方法，提供显示圆面积的方法
            3)编程创建一个Cale计算类，在其中定义2个变量表示两个操作数，定义四个方法实现求差、和、乘、商(除数为0要提示)
              并创建两个对象，分别测试
         */

        A03 a03 = new A03();
        int[] arr = {1, 2, 3, 4, 5};
        int[] newArr = a03.copyArr(arr);
        if (newArr != null) {
            System.out.println("复制后数组元素如下:");
            for (int i = 0; i < newArr.length; i++) {
                System.out.print(newArr[i] + " ");
            }
        } else {
            System.out.println("原数组为空，请重新输入");
        }

        Circle circle = new Circle(2.2);
        System.out.println("圆的信息：半径=" + circle.radius + "，周长=" + circle.getPerimeter() + ",面积=" + circle.getArea());


//        Cale cale = new Cale(4, 2);
        Cale cale = new Cale(4, 0);
        double sum = cale.getSum();
        System.out.println(cale.a + "与" + cale.b + "相加结果=" + sum);
        double differencing = cale.getDifferencing();
        System.out.println(cale.a + "与" + cale.b + "相减结果=" + differencing);
        double multiplication = cale.getMultiplication();
        System.out.println(cale.a + "与" + cale.b + "相乘结果=" + multiplication);
        Double division = cale.getDivision();
        if (division != null) {
            System.out.println(cale.a + "与" + cale.b + "相除结果=" + division);
        } else {
            System.out.println("除数不能为0");
        }

    }
}

class A03 {

    public int[] copyArr(int[] arr) {
        if (arr.length > 0) {
            int[] returnArr = new int[arr.length];
            for (int i = 0; i < arr.length; i++) {
                returnArr[i] = arr[i];
            }
            return returnArr;
        } else {
            return null;
        }

    }
}

class Circle {

    double radius;

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getPerimeter() {
        return 2 * Math.PI * radius;
    }

    public double getArea() {
        return Math.PI * radius * radius;
    }
}

class Cale {

    double a;

    double b;

    public Cale(double a, double b) {
        this.a = a;
        this.b = b;
    }

    public double getSum() {
        return this.a + this.b;
    }

    public double getDifferencing() {
        return this.a - this.b;
    }

    public double getMultiplication() {
        return this.a * this.b;
    }

    public Double getDivision() {
        if (this.b == 0) {
            return null;
        } else {
            return this.a / this.b;
        }
    }
}